<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">若依后台管理系统</h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>

        </el-button>
      </el-form-item>



      <!--商户注册-->
      <el-button style="width:100%;" @click="registerBussiness">商户注册</el-button>

      <el-dialog
        title="商户注册"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="联系人姓名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="ruleForm.mobile"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="msg">
              <el-input v-model="ruleForm.msg" style="width: 60%;"></el-input>
              <el-button type="primary" @click="getMsg">获取验证码</el-button>
          </el-form-item>
          <el-form-item label="请输入密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="请再次输入密码" prop="rePassword">
            <el-input v-model="ruleForm.rePassword"></el-input>
          </el-form-item>
          <el-form-item prop="type">
            <el-radio-group v-model="ruleForm.type">
              <el-radio label="0" value="0">个人职业房东</el-radio>
              <el-radio label="1" value="1">公司化公寓</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="ck">
              <el-checkbox v-model="ruleForm.ck"><span>已阅读并同意<a href="https://wenku.baidu.com/view/fa1da813b91aa8114431b90d6c85ec3a87c28bb9.html">维数公寓长组公寓服务协议</a></span></el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <el-dialog
        title="注册成功,请填写详细信息"
        :visible.sync="dialogVisible1"
        width="30%"
        :before-close="handleClose">
        <el-form :model="ruleForm1" :rules="rules1" ref="ruleForm1" label-width="100px" class="demo-ruleForm">
          <el-form-item label="公司名称" prop="name">
            <el-input v-model="ruleForm1.name"></el-input>
          </el-form-item>
          <el-form-item label="负责人姓名" prop="corporation">
            <el-input v-model="ruleForm1.corporation"></el-input>
          </el-form-item>
          <el-form-item label="身份证号" prop="certNum">
            <el-input v-model="ruleForm1.certNum"></el-input>
          </el-form-item>
          <el-form-item label="身份证照片">
              <el-upload
                class="avatar-uploader"
                :action=this.$picUrl
                :show-file-list="false"
                :on-success="handleAvatarSuccess">
                <img width="50px" height="50px" v-if="ruleForm1.certPicture" :src="ruleForm1.certPicture" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
              <el-upload
                class="avatar-uploader"
                :action="this.$picUrl"
                :show-file-list="false"
                :on-success="handleAvatarSuccess2">
                <img width="50px" height="50px" v-if="ruleForm1.certPicture2" :src="ruleForm1.certPicture2" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
              <el-upload
                class="avatar-uploader"
                :action=this.$picUrl
                :show-file-list="false"
                :on-success="handleAvatarSuccess3">
                <img width="50px" height="50px" v-if="ruleForm1.certPicture3" :src="ruleForm1.certPicture3" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
          </el-form-item>
          <el-form-item v-if="ruleForm.type==1" label="营业执照"><el-upload
            class="avatar-uploader"
            :action=this.$picUrl
            :show-file-list="false"
            :on-success="handleAvatarSuccess4">
            <img width="50px" height="50px" v-if="ruleForm1.url" :src="ruleForm1.url" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload></el-form-item>
          <el-form-item label="办公地址" prop="rePassword">
            <div class="block">
              <el-cascader
                v-model="value"
                :options="options"
                :props="{label:'regionName',value:'regionId'}"
                @change="handleChange"></el-cascader>
            </div>
          </el-form-item>
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="ruleForm1.address"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm1('ruleForm1')">提交</el-button>
            <el-button @click="resetForm1('ruleForm1')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-form>


    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2020 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
  import {getCodeImg} from "@/api/login";
  import {getMessage,register,getCity,registerBusiness,getIp} from "@/api/system/business";
  import Cookies from "js-cookie";
  import {encrypt, decrypt} from '@/utils/jsencrypt'
  import {v4} from 'uuid'

  export default {
    name: "Login",
    data() {
      return {
        baseUrl:'',
        value:[],
        options:[],
        dialogVisible: false,
        dialogVisible1: false,
        ruleForm: {},
        rules: {
          mobile: [
            {required: true, message: '请输入手机号', trigger: 'blur'},
            {min: 11, max: 11, message: '长度在 11 个字符', trigger: 'blur'}
          ],
          msg: [
            {required: true, message: '请输入验证码', trigger: 'blur'},
          ],
          ck: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          resource: [
            { required: true, message: '请选择类型', trigger: 'change' }
          ]
        },
        ruleForm1: {certPicture:'',certPicture2:'',certPicture3:'',url:''},
        rules1: {},
        codeUrl: "",
        cookiePassword: "",
        loginForm: {
          username: "admin",
          password: "admin123",
          rememberMe: false,
          code: "",
          uuid: ""
        },
        loginRules: {
          username: [
            {required: true, trigger: "blur", message: "用户名不能为空"}
          ],
          password: [
            {required: true, trigger: "blur", message: "密码不能为空"}
          ],
          code: [{required: true, trigger: "change", message: "验证码不能为空"}],
        },
        loading: false,
        redirect: undefined
      };
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      this.getCode();
      this.getCookie();
    },
    methods: {
      getCity() {
        getCity().then(res=>{
          this.options=res;
        })
      },
      handleChange(value) {
        this.ruleForm1.province=value[0];
        this.ruleForm1.city=value[1];
        this.ruleForm1.county=value[2];
        console.log(value);
      },
      handleAvatarSuccess(res) {
        this.ruleForm1.certPicture = res;
      },
      handleAvatarSuccess2(res) {
        this.ruleForm1.certPicture2 = res;
      },
      handleAvatarSuccess3(res) {
        this.ruleForm1.certPicture3 = res;
      },
      handleAvatarSuccess4(res) {
        this.ruleForm1.url = res;
      },
      getMsg() {
        getMessage(this.ruleForm).then(res=>{
          if(res.code==500){
            this.$message.error(res.msg);
          }
          this.$message.success("获取验证码成功")
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            register(this.ruleForm).then(res=>{
              if(res.code==200){
                this.$message.success(res.msg);
                this.ruleForm1.mobile=res.data;
                this.dialogVisible=false;
                this.dialogVisible1=true;
                this.getCity();
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      submitForm1(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            registerBusiness(this.ruleForm1).then(res=>{
              if(res.code==200){
                this.$message.success(res.msg);
                this.dialogVisible1=false;

              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm1(formName) {
        this.$refs[formName].resetFields();
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      getIp() {
        getIp().then(res=>{
          this.ruleForm.ip=res.data;
        })
      },
      registerBussiness() {
        this.dialogVisible=true;
        this.ruleForm={}
        this.getIp();
        this.ruleForm.token=v4();
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`);
      },
      getCode() {
        getCodeImg().then(res => {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        });
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        };
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, {expires: 30});
              Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
              Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            this.$store
              .dispatch("Login", this.loginForm)
              .then(() => {
                this.$router.push({path: this.redirect || "/"});
              })
              .catch(() => {
                this.loading = false;
                this.getCode();

              });
          }
        });
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/image/login-background.jpg");
    background-size: cover;
  }

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
    width: 33%;
    height: 38px;
    float: right;

    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }

  .login-code-img {
    height: 38px;
  }
</style>
